/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculator;

import dao.CityDAOImpl;
import hibernate.entityes.City;
import result.ResultCalculation;

/**
 *
 * @author eleven
 */
public class CrowflightCalculator implements Crowflight {

    @Override
    public ResultCalculation calculate(String fromCity, String toCity) {
        CityDAOImpl daocity = dao.DAOFactory
                .getHibernateDAOFactory()
                .getCityDAOImpl();

        City fromcity = daocity.getCitiesByName(fromCity).get(0);
        City tocity = daocity.getCitiesByName(toCity).get(0);

        double lat1 = fromcity.getLatitude() * Math.PI / 180;
        double long1 = fromcity.getLongitude() * Math.PI / 180;
        double lat2 = tocity.getLatitude() * Math.PI / 180;
        double long2 = tocity.getLongitude() * Math.PI / 180;

        double distance = 6372795
                * Math.acos(Math.sin(lat1) * Math.sin(lat2)
                + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2));
        return new ResultCalculation(fromCity, toCity, getType(), distance);
    }

    @Override
    public TypeCalculator getType() {
        return new TypeCalculator("Crowflight Calculator");
    }
}
